<template>
  <div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" :id="id">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 填写票号 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <table class="table table-striped table-hover table-sm">
            <thead>
              <tr>
                <th>姓名</th>
                <th>票号</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(info, index) in passengers">
                <td>
                  {{ info.name }}
                </td>
                <td>
                  <input
                    type="text"
                    class="form-control"
                    placeholder="票号"
                    v-model="info.ticketNo"
                  />
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="modal-footer">
         <button
            type="button"
            class="btn btn-secondary"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  props: {
    id: {
      type: String,
      default: "fillTicketNoModal"
    }
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      passengers: [],

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.getMyModal().hide();
      this.resolve(this.passengers);
    },
    modal: function (passengers) {
      this.getMyModal().show();
      this.passengers = passengers;

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl);
      }

      return this.myModal;
    },
  },
};
</script>
